--RedzoneRPG
--by Kylegar

local meta = FindMetaTable("Player")

function meta:InitPlayer()
  self.Experiance =0
  self.Level = 0
  self.sp = 0
  self.XPForNextLevel = 0
  self.rzMaxHealth = 0
  self.rzMaxArmor = 0
  self.rzArmor = 0
  self.SkillGunnery = 0
  self.SkillSupport = 0
  self.SkillMedic = 0
  self.SkillEngineer = 0
  self.rzMaxDeployables = 0
  
end

function meta:ErrorMessage(str)
    if!(type(str)==string) then return end
	self:PrintMessage(HUD_PRINTTALK, "Error: "..str)
	for k,v in pairs(player.GetAll()) do
		if(v:IsAdmin()) then
		 v:PrintMessage(HUD_PRINTTALK, "Player "..self:GetName().." Had Error: "..str)
		end
	end
end

function meta:DebugMessage(str)
 if!(type(str)==string) then return end
 if!(self.rzDebugVar) then return end
 self:PrintMessage(HUD_PRINTTALK, "Debug Message: "..str)
 
end

function AllowDebugMessages(ply,cmd,args)
	if(args[1]==1) then ply.rzDebugVar = true Msg("Allowing Debug Messages\n")end
	if(args[1]==0) then ply.rzDebugVar = false end
end
concommand.Add("rz_debug_AllowDebugMessages", AllowDebugMessages)

function meta:SetXP(xp)
	self.Experiance = xp
	self:SetNWInt(Experiance , self.Experiance)
end

function meta:AddXP(xp)
	self.Experiance = self.Experiance + xp
	self:SetNWInt(Experiance , self.Experiance)
end

function meta:GetXP()
	self.Experiance = self:GetNWInt(Experiance)
	if!(self.Experiance) then return 0 end
	return self.Experiance
	
end

function meta:SetLevel(level)
	self.Level = level
	self:SetNWInt(Level , self.Level)
end

function meta:AddLevel(level)
	self.Level = self.Level + level
	self:SetNWInt(Level , self.Level)
end

function meta:GetLevel()
	self.Level=self:GetNWInt(Level)
	if !(self.Level) then return 0 end
	return self.Level
end


function meta:LevelUp()
	self:SetXP((self:GetXP() - self:GetXPForNextLevel()))
	if(self:GetXP() < 0) then self:SetXP(0) end
	self:AddLevel(1)
	self:SetXPForNextLevel()
	self:AddSkillPoints(1)
end
	

function meta:SetXPForNextLevel( )
	local plylevel = self:GetLevel()
	local x , y = 0 , plylevel
	
	if(plylevel>=10) then	
		x = math.floor(plylevel*.1)
		y = y - (x*10)	
		x = 10+(x-1)
	end	
	self.XPForNextLevel = ((x*1024)*100+(2^y)*100)
	self:SetNWInt(Xpfornextlevel , self.XPForNextLevel)
end

function meta:GetXPForNextLevel()
	self.XPForNextLevel = self:GetNWInt(Xpfornextlevel)
	return self.XPForNextLevel
end

function meta:SetSkillPoints(sp)
	self.sp = sp
	self:SetNWInt(SkillPoints , self.sp)
end

function meta:AddSkillPoints(sp)
	if!(self.sp) then self.sp = 0 end
	self.sp = self.sp + sp
	self:SetNWInt(SkillPoints , self.sp)
end

function meta:GetSkillPoints()
	self.sp = self:GetNWInt(SkillPoints)
	return self.sp
end

function meta:HasSkillPoints()
	if(self:GetSkillPoints() > 0) then return true end
	return false	
end

function meta:SetPlayerMaxHealth(hp)
	self.rzMaxHealth = hp
	self:SetNWInt("rzmaxhealth" , self.rzMaxHealth)
end

function meta:AddPlayerMaxHealth( hp )
	if!(self.rzMaxHealth) then self.rzMaxHealth = 100 end
	self.rzMaxHealth = self.rzMaxHealth + hp
	self:SetNWInt("rzmaxhealth" , self.rzMaxHealth)
end

function meta:GetPlayerMaxHealth()
	self.rzMaxHealth = self:GetNWInt("rzmaxhealth")	
	return self.rzMaxHealth
end

function meta:SetPlayerMaxArmor(armor)
	self.rzMaxArmor = armor
	self:SetNWInt("maxarmor" , self.rzMaxArmor)
end

function meta:AddPlayerMaxArmor(armor)
	if!(self.rzMaxArmor) then self.rzMaxArmor = 0 end
	self.rzMaxArmor = self.rzMaxArmor + armor
	self:SetNWInt("maxarmor" , self.rzMaxArmor)
end

function meta:GetPlayerMaxArmor()
	if!(self.rzMaxArmor) then self.rzMaxArmor = 1 end
	self.rzMaxArmor = self:GetNWInt("maxarmor")	
	return self.rzMaxArmor
end

function meta:SetPlayerArmor(armor)
	self.rzArmor = armor
	self:SetNWInt("armor", self.rzArmor)
end

function meta:AddPlayerArmor(armor)
	if!(self.rzArmor) then self.rzArmor = 0 end
	self.rzArmor = self.rzArmor + armor
	self:SetNWInt("armor" , self.rzArmor)
end

function meta:GetPlayerArmor()
	self.rzArmor = self:GetNWInt("armor")
	if!(self.rzArmor) then self.rzArmor = 0 end
	return self.rzArmor
end

function meta:SetPlayerClass(classnum)
	self.rzPlayerClass = classnum
	self:SetNWInt("playerclass", self.rzPlayerClass)
end

function meta:GetPlayerClass()
	self.rzPlayerClass = self:GetNWInt("playerclass")
	if!(self.rzPlayerClass()) then self.rzPlayerClass = 0 end
	return self.rzPlayerClass
end

function meta:SetBaseSkillLevel(skill , level)
		
	if(skill == "Gunner") then
		self.SkillGunnery = level
		self:SetNWInt("skill_Gunner" , self.SkillGunnery)
		return
	end
	if(skill == "Support") then
		self.SkillSupport = level
		self:SetNWInt("skill_Gupport" , self.SkillSupport)
		return
	end
	
	if(skill == "Medic") then
		self.SkillMedic = level
		self:SetNWInt("skill_Medic" , self.SkillMedic)
		return
	end
	if(skill == "Engineer") then
		self.SkillEngineer = level
		self:SetNWInt("skill_Engineer" , self.SkillEngineer)
		return
	end
	
	
	Msg("Skill "..skill.." Unknown.\n")
	
	
end

function meta:AddBaseSkillLevel(skill , level)
	
	if(skill == "Gunner") then
		self.SkillGunnery = self.SkillGunnery + level
		self:SetNWInt("skill_Gunner" , self.SkillGunnery)
		return
	end
	if(skill == "Support") then
		self.SkillSupport = self.SkillSupport + level
		self:SetNWInt("skill_Gupport" , self.SkillSupport)
		return
	end
	if(skill=="Medic") then
		self.SkillMedic = self.SkillMedic + level
		self:SetNWInt("skill_Medic" , self.SkillMedic)
		return
	end	
	if(skill=="Engineer") then
		self.SkillEngineer = self.SkillEngineer + level
		self:SetNWInt("skill_Engineer" , self.SkillEngineer)
		return
	end
		
	Msg("Skill "..skill.." Unknown\n")

end


function meta:GetBaseSkillLevel(skill)

	if(skill == "Gunner") then
		self.SkillGunnery = self:GetNWInt("skill_Gunner")
		return self.SkillGunnery	
	end
	
	if(skill == "Support") then
		self.SkillSupport = self:GetNWInt("skill_Gupport")
		return self.SkillSupport 
	end
	
	if(skill == "Medic") then
		self.SkillMedic = self:GetNWInt("skill_Medic")
		return self.SkillMedic 	
	end
	
	if(skill == "Engineer") then
		self.SkillEngineer = self:GetNWInt("skill_Engineer")	
		return self.SkillEngineer
	end

	self:ErrorMessage("Skill: "..skill.." Unknown.")
	return nil
	
end



function meta:DoBaseSkillChange()
	self:SetPlayerMaxHealth(100)
	self:SetPlayerMaxArmor(100)
	
		
	--Gunner Skill Point Additions
	for i=0 , self:GetBaseSkillLevel("Gunner") , 1 do
	 self:AddPlayerMaxHealth(20)
	 self:AddPlayerMaxArmor(10)	
	end
	--Support Skill Point Additions
	for i=0, self:GetBaseSkillLevel("Support"), 1 do
	  self:AddPlayerMaxHealth(5)
    end	  
	
	
	--Engineer Skill Point Additions
	for i=0, self:GetBaseSkillLevel("Engineer"), 1 do
	 self:AddPlayerMaxHealth( 5 )	
	 self:AddMaxDeployedObjects(2)
	end
	--Medic Skill Point Additions
	for i=0, self:GetBaseSkillLevel("Medic"), 1 do
	 self:AddPlayerMaxHealth( 5 )	
	 
	end
	
	
	--Overall Things
	self:SetHealth(self:GetPlayerMaxHealth())
	

end

function meta:SetMaxDeployedObjects(num)
	self.rzMaxDeployables = num
	self:SetNWInt("deployables" , self.rzMaxDelployables)
end

function meta:AddMaxDeployedObjects(num)
	if!(self.rzMaxDeployables) then self.rzMaxDeployables = 0 end
	self.rzMaxDeployables = self.rzMaxDeployables + num
	self:SetNWInt("deployables" , self.rzMaxDelployables)
end

function meta:GetMaxDeployedObject()
	self.rzMaxDeployables = self:GetNWInt("deployables")
	return self.rzMaxDeployables
end

function meta:CreateInvitory()
	self.rzInv = {}
end

function meta:RemoveFromInv(item)
	for k,v in pairs(self.rzInv) do
		if (table.HasValue(v, item.Classname)) then
			table.remove(self.rzInv, k)
		end
	end
end

function meta:AddToInv(item)
	for k,v in pairs(self.rzInv) do
			if (table.HasValue(v,item.Classname)) then
				v.Ammount=v.Ammount+1
				--item:Remove()
				rzItem.RemoveItem(item)
				return
			end
		end
	
	item.Owner = self
	table.insert(self.rzInv , item)
	return
	
	
end

function meta:GetInv()
	return self.rzInv
end

function meta:UseItem(item)
	Msg(type(item.OnUse))
	item.OnUse()
end

function meta:SetCamera(ent)
	self.rzCam = ent
end

function meta:GetCamera()
	return self.rzCam
end
